package edu.hawaii.duedates.webApp;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;
import edu.hawaii.duedates.configuration.DueDatesConfiguration;
import edu.hawaii.duedates.configuration.User;

/**
 * This is a test class for ResultPage.java.
 * 
 * @author Mari-Lee Flestado, Aric West, Tyler Wolff
 *
 */
public class TestResultPage {
  
  private DueDatesConfiguration config;
  
  /**
   * Sets up a mock DueDatesConfiguration object for use in
   * tests.
   */
  @Before
  public void setUp() {
    this.config = new DueDatesConfiguration();
    User testUser = new User("Test", "tester@hawaii.edu", "password");
    
    // login with someone who has 2 books out
    String login = System.getenv("DUEDATES_TESTUSER2_ID");
    String password = System.getenv("DUEDATES_TESTUSER2_LASTNAME");
    testUser.setUhManoa("-uhm " + login + " " + password);
    this.config.addUser(testUser);
    this.config.setSmtpServer("mail.hawaii.edu");  

    // hsl user
    User testUser2 = new User("Test2", "tester2@hawaii.edu", "password2");
    testUser2.setHawaiiStateLib("-hsl 2342 2342");
    this.config.addUser(testUser2);
  }
  
  /**
   * Testing the basic functionality of the Result Page class.
   */
  @Test
  public void testResultPage() {
    
    final String RESULT_FORM = "resultForm";
    
    // Start up the WicketTester and check that the start page renders. 
    WicketTester tester = new WicketTester(new DueDatesApplication(config));
    tester.startPage(Index.class);
    
    // Have to run through login to correctly create user in session
    FormTester formTester = tester.newFormTester("loginForm");
    formTester.setValue("LoginEmail", "tester@hawaii.edu");
    formTester.setValue("LoginPassword", "password");
    formTester.submit("Login");
    tester.assertRenderedPage(ResultPage.class);
    
    // Now check that the links all exist. 
    tester.assertComponent("Results", Link.class);
    tester.assertComponent("Alerts", Link.class);
    tester.assertComponent("Logout", Link.class);
    
    //Check wicket id
    assertNotNull("Testing wicket:id Results found.", tester.getTagByWicketId("Results"));
    assertNotNull("Testing wicket:id Alerts found", tester.getTagByWicketId("Alerts"));
    assertNotNull("Testing wicket:id Logout found", tester.getTagByWicketId("Logout"));
    assertNotNull("Testing wicket:id uhContainer found", tester.getTagByWicketId("uhContainer"));
    assertNotNull("Testing wicket:id updateUh found", tester.getTagByWicketId("updateUh"));
    assertNotNull("Testing wicket:id uhLabel found", tester.getTagByWicketId("uhLabel"));
    assertNotNull("Testing wicket:id withinUh found", tester.getTagByWicketId("withinUh"));
    
    // Test that the update hsl link is not there
    tester.assertInvisible("resultForm:hslContainer");
    assertFalse("no hsl info", tester.getTagByWicketId(RESULT_FORM).getMarkup().
        contains("hslContainer"));
   
    // Test form input and update button
    FormTester formTester2 = tester.newFormTester("resultForm");
    formTester2.setValue("uhContainer:withinUh", "0");
    formTester2.submit("uhContainer:updateUh");   
    tester.assertRenderedPage(ResultPage.class);
    
    // Test that data output shows up
    assertTrue("items exist", tester.getTagByWicketId(RESULT_FORM).getMarkup().
        contains("itemTable"));
    
    assertNotNull("Testing wicket:id library found.", tester.getTagByWicketId("library"));
    assertNotNull("Testing wicket:id titile found", tester.getTagByWicketId("title"));
    assertNotNull("Testing wicket:id duedate found", tester.getTagByWicketId("duedate"));
   
    // Tests the alerts and logout link
    tester.clickLink("Results");
    tester.assertRenderedPage(ResultPage.class);
    tester.clickLink("Alerts");
    tester.assertRenderedPage(AlertsPage.class);
    tester.clickLink("Logout");
    tester.assertRenderedPage(Index.class);
    
    WicketTester tester2 = new WicketTester(new DueDatesApplication(config));
    tester2.startPage(Index.class);
    
    // Tests hsl user
    FormTester formTester3 = tester2.newFormTester("loginForm");
    formTester3.setValue("LoginEmail", "tester2@hawaii.edu");
    formTester3.setValue("LoginPassword", "password2");
    formTester3.submit("Login");
    tester2.assertRenderedPage(ResultPage.class);
    tester2.assertInvisible("resultForm:uhContainer");
    
    // Test form input and update button
    FormTester formTester4 = tester2.newFormTester("resultForm");
    formTester4.setValue("uhContainer:withinHsl", "0");
    formTester4.submit("uhContainer:updateHsl");   
    tester2.assertRenderedPage(ResultPage.class);
  }

}
